<?php
class Amember {
	const MAXLEN = 15;
	public static function main($funcode) {
		switch ($funcode) {
			case 1:
				sview::verify();
				self::__doguards();
				break;
			case 2:
				sview::verify();
				self::__doviewer();
				break;
			case 3:
				sview::verify();
				self::__contributer();
				break;
			case 4:
				sview::verify();
				self::__dovcard();
				break;
			case 5:
				sview::verify(true, true);
				self::__setmnger();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	//守护会员5-3-1
	private static function __doguards() {
		sview::$ctrl['studioid'] = intval(sview::$ctrl['studioid']);
		$list  = array();
		$users = api_guard::user(sview::$ctrl['studioid']);
		if ($users) {
			$uorm = model::load('live/userinfo');
			foreach ($users as &$item) {
				$user   = $uorm->set_devision($item['userid'])->get_one($item['userid']);
				$list[] = array('userid'=>$user['userid'], 'nickname'=>htmlspecialchars_decode($user['nickname']), 'avtar'=>$user['thumb'],
					'exp'=>$user['exp'], 'lv'=>$user['lv'], 'roler'=>api_studio::isanchor($item['userid']));
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//观众会员5-3-2
	private static function __doviewer() {
		$studioid            = intval(sview::$ctrl['studioid']);
		sview::$ctrl['uids'] = trim(sview::$ctrl['uids']);
		sview::$ctrl['uids'] = explode(',', sview::$ctrl['uids']);
		sview::$ctrl['uids'] = array_filter(sview::$ctrl['uids'], 'is_numeric');
		$list = array();
		if (sview::$ctrl['uids'] && count(sview::$ctrl['uids']) > 0) {
			$uorm = model::load('live/userinfo');
			$uids = api_guard::uids($studioid);
			$rds  = cache::getinst('redis')->instance();
			foreach (sview::$ctrl['uids'] as $userid) {
				if (!is_numeric($userid) || intval($userid) < 1 || $userid > api_im::INTBASE) {
					continue;
				}
				$user   = $uorm->set_devision($userid)->get_one($userid);
				if (!$user) {
					continue;
				}
				$motorid= 0;
				if ($user['motorid'] > 0) {
					$where = array('stid'=>$studioid, 'reftype'=>1, 'userid'=>$userid);
					$motor = model::load('live/userprops')->set_devision($userid)->get_item($where);
					if ($motor && $motor['expire'] > config::$nowtime) {
						$motorid = $motor['refid'];
					}
				}
				$access = (int)api_access::get_access($userid, $studioid); 
				$isguard= ($uids && in_array($userid, $uids))? 1:0;	
				$coin   = api_studio::evtonestat($studioid, $userid, $rds);	
				$list[] = array('userid'=>$user['userid'], 'nickname'=>htmlspecialchars_decode($user['nickname']), 'motorid'=>$motorid, 'access'=>$access, 'isguard'=>$isguard, 'coin'=>$coin,
					'avtar'=>$user['thumb'], 'lv'=>$user['lv'], 'exp'=>$user['exp'], 'roler'=>api_studio::isanchor($user['userid']));
			}
		}
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//直播场次贡献榜5-3-3
	private static function __contributer() {
		$studioid  = intval(sview::$ctrl['studioid']);
		$offset    = intval(sview::$ctrl['offset']);
		$limit     = intval(sview::$ctrl['limit']);
		if ($limit > 30 ) {
			$limit = 10;
		}
		$rds  = cache::getinst('redis')->instance();
		$list = api_studio::evtrank($studioid, $offset, $limit, $rds);
		sview::$view->assign('datasets', $list);
		sview::$view->display();
	}
	
	//观众会员卡片5-3-4
	private static function __dovcard() {
		$userid   = intval(sview::$ctrl['userid']);
		$studioid = intval(sview::$ctrl['studioid']);
		$uorm     = model::load('live/userinfo');
		if ($userid < 1 || $userid > api_im::INTBASE) {
			sview::$view->assign_error(80009, lang::text(80009));
		}
		$sorm     = model::load('live/studio');
		$user     = $uorm->set_devision($userid)->get_one($userid);
		$lv       = api_lv::item($user['lv'], 1);
		$rsexp    = 0;
		if ($user['lv'] > 1) {
			$plv   = api_lv::item($user['lv'] - 1, 1);
			$rsexp = $plv['coin'];
		}
		$access   = 0;
		if ($studioid > 0) {
			$access = (int)api_access::get_access($userid, $studioid);
		}
		$birth    = strtotime($user['birth']);
		if (!$birth) {
			$birth = strtotime('1991-01-01');
		}
		$city  = empty($user['city'])? 'UnKnow':$user['city'];
		$data = array('userid'=>$user['userid'], 'nickname'=>htmlspecialchars_decode($user['nickname']), 'avtar'=>$user['thumb'], 'roler'=>0,
			'rlv'=>$user['lv'], 'rlvicon'=>$lv['iconsrc'], 'rexp'=>$user['exp'], 'rsexp'=>$rsexp, 'reexp'=>$lv['coin'],
			'location'=>$city, 'access'=>$access, 'gender'=>$user['sex'], 'birthday'=>$birth,  'isfollow'=> 0, 'xfans'=>0
		);
		$studio   = $sorm->get_item(array('userid'=>$userid, 'status'=>1));
		if ($studio) {
			$aorm  = model::load('live/anchor');
			$anchor= $aorm->get_one($userid);
			$data['roler'] = 1;
			$data['xfans'] = $anchor['xfans'];
			$data['alv']   = $user['lv'];
			$data['aexp']  = $user['exp'];
			$lv     = api_lv::item($user['lv'], 0);
			$rsexp  = 0;
			if ($user['lv'] > 1) {
				$plv   = api_lv::item($user['lv'] - 1, 0);
				$rsexp = $plv['coin'];
			}
			$data['asexp']  = $rsexp;
			$data['aeexp']  = $lv['coin'];
			$data['alvicon']= $lv['iconsrc'];
			$acl            = acl::getinst('vedio');
			if ($acl->user && $userid != $acl->user['id']) {
				$data['isfollow'] = api_stats::hattens($acl->user['id'], $userid);
			}
		}
		sview::$view->assign('datasets', $data);
		sview::$view->display();
	}
	
	//设置管理员
	private static function __setmnger() {
		$userid  = intval(sview::$ctrl['userid']);
		$nuserid = acl::getinst('vedio')->user['id'];
		$sid     = (int)sview::$ctrl['studioid'];
		$studio = model::load('live/studio')->get_one($sid);
		if (!$studio || $studio['userid'] != $nuserid || $userid < 1) {
			sview::$view->assign_error(80011, lang::text(80011));
		}
		$ismnger= (int)sview::$ctrl['ismanager'];
		$fields = array('userid'=>$userid, 'studioid'=>$sid);
		$sorm   = model::load('live/stumanager')->set_devision($sid);
		$mnger  = $sorm->get_item($fields);
		if ($ismnger == 1 && !$mnger) {
			//直播间房管认识的限制
			$ttl= $sorm->get_total(array('studioid'=>$sid));
			if ($ttl >= self::MAXLEN) {
				sview::$view->assign_error(80070, lang::replace(80070, self::MAXLEN));
			}
			$fields['stime'] = config::$nowtime;
			$sorm->new_one($fields);
		} else if ($ismnger == 0 && $mnger) {
			$sorm->delete($mnger['id']);
			//取消管理员设置的时候消息提醒
			if (config::$nowtime - $mnger['stime'] > 300) {
				$user = model::load('live/userinfo')->set_devision($nuserid)->get_one($nuserid);
				$msg  = lang::replace(80065, $user['nickname'], router::get_url('live', $studio['studiono']));
				api_sitemsg::send(lang::text(80064), $msg, $userid, 3, $nuserid);
			}
		}
		//发送广播
		$access = api_access::reset($userid, $sid);
		$data = array('userid'=>$userid, 'admin'=>$nuserid, 'access'=>$access, 'ismanager'=>$ismnger, 'setmng'=>1);
		api_im::notifystudio($sid, json_encode($data), 0, 20019);
		sview::$view->display();
	}
}